﻿using ASPApiDemo.Common.Cache;
using ASPApiDemo.Common.Core;
using ASPApiDemo.Common.Option;
using ASPApiDemo.IService;
using ASPApiDemo.Model;
using ASPApiDemo.Service;
using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;

namespace ASPApiDemo.Controllers
{
    // ASP.NET Core 默认使用 DefaultControllerFactory 来创建 Controller
    [Route("api/[controller]")]
    [ApiController]
    public class RoleController : ControllerBase
    {
        private readonly ILogger<RoleController> logger;
        private readonly IBaseService<Role, RoleVo> roleService;
        private readonly IServiceScopeFactory scopeFactory;
        private readonly IOptions<RedisOptions> _appSettings;
        private readonly ICaching _caching;
        //通过属性注入
        public IBaseService<Role, RoleVo> roleServiceObj { get; set; }
        //通过构造函数注入
        public RoleController(ILogger<RoleController> logger,
            IBaseService<Role, RoleVo> roleService, IServiceScopeFactory scopeFactory,
            IOptions<RedisOptions> appSettings, ICaching caching)
        {
            this.logger = logger;
            this.roleService = roleService;
            this.scopeFactory = scopeFactory;
            _appSettings = appSettings;
            _caching = caching;
        }
        [HttpGet(Name = "GetRole")]
        public async Task<object> Get()
        {
            //通过注入scope获取服务,当然也可以直接注入容器IserviceProvider
            //using var scope =scopeFactory.CreateScope();
            //scope.ServiceProvider.GetService<Role>();
            //通过构造函数注入服务
            var roleList = await roleService.Query();
            //var roleList = await roleServiceObj.Query();
            // var redisOption = _appSettings.Value;
            // 演示通过容器单例获取Option，而不是构造函数注入
            var redisOption = AppService.GetOptions<RedisOptions>();
            Console.WriteLine(JsonConvert.SerializeObject(redisOption));
            // 演示通过容器单例直接获取服务层的服务，而不是在构造函数中注入
            var roleServiceBySp = AppService.GetService<IBaseService<Role, RoleVo>>(false);
            var rolelists = await roleServiceBySp.Query();
            //测试分布式缓存
            var cacheKey = "cache-key";
            List<string> cacheKeys = await _caching.GetAllCacheKeysAsync();
            await Console.Out.WriteLineAsync("全部keys -->" + JsonConvert.SerializeObject(cacheKeys));

            await Console.Out.WriteLineAsync("添加一个缓存");
            await _caching.SetStringAsync(cacheKey, "hi laozhang");
            await Console.Out.WriteLineAsync("全部keys -->" + JsonConvert.SerializeObject(await _caching.GetAllCacheKeysAsync()));
            await Console.Out.WriteLineAsync("当前key内容-->" + JsonConvert.SerializeObject(await _caching.GetStringAsync(cacheKey)));

            await Console.Out.WriteLineAsync("删除key");
            await _caching.RemoveAsync(cacheKey);
            await Console.Out.WriteLineAsync("全部keys -->" + JsonConvert.SerializeObject(await _caching.GetAllCacheKeysAsync()));

            return rolelists;
        }
    }
}